package com.codingfuture.erp_system.web.controller;

import com.codingfuture.erp_system.dto.GoodsDto;
import com.codingfuture.erp_system.dto.PageData;
import com.codingfuture.erp_system.entity.Goods;
import com.codingfuture.erp_system.entity.GoodsType;
import com.codingfuture.erp_system.service.GoodsService;
import com.codingfuture.erp_system.util.CheckUtils;
import com.codingfuture.erp_system.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Slf4j
@RequestMapping("/v2")
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    private final String str = "";

    /**
     * id查
     *
     * @return gooods
     */

    @GetMapping("/goods/selectone")
    public Result<HashMap<Object, Object>> findById(Integer id, HttpSession session) {
        session.setAttribute("goodsUuid", id);
        Goods goods = goodsService.findById(id);
        return Result.OK(goods);
    }

    /**
     * 数据渲染和分页  模糊搜索
     *
     * @param page
     * @param rows
     * @param goodsDto
     * @return 全部数据
     */
    @GetMapping("/goods/listByPage")
    public Result<?> findByAll(@RequestParam(defaultValue = "1") int page,
                               @RequestParam(defaultValue = "10") int rows,
                               GoodsDto goodsDto) {
        Page<Object> page1 = PageHelper.startPage(page, rows);
        List<Goods> byAll = goodsService.findByAll(goodsDto);
        PageData<Goods> data = new PageData<>(byAll, page1.getTotal());
        return Result.ok(data);
    }

    /**
     * 获取商品类型
     *
     * @return GoodsType
     */

    @GetMapping("/goods/goodstypelist")
    public Result findAll() {
        List<GoodsType> list = goodsService.findAll();

        return Result.OK(list);
    }

    /**
     * 添加
     *
     * @param goods
     * @return 成功？失败？
     */

    @PostMapping("/goods/add")
    public Result insert(Goods goods) {
        Integer goodsType = goods.getGoodsType();
        if (goodsType != null) {
            goods.setGoodsTypeUuid(goodsType);
            String name = goods.getName();
            Double inPrice = goods.getInPrice();
            Double outPrice = goods.getOutPrice();
            String unit = goods.getUnit();
            if (name == null && inPrice == null && outPrice == null && unit == null
                    || Objects.equals(name, str) || Objects.equals(unit, str)) {
                return Result.error(400, "请修输入全部参数");
            }
            //校验名称合法性
            else if (CheckUtils.checkChinese(goods.getName())) {
                return Result.error(500, "名称不是中文/过长");
            }
            //校验产地合法性
            else if (CheckUtils.checkChinese(goods.getOrigin())) {
                return Result.error(500, "产地不是中文/过长");
            }
            //校验厂家合法性
            else if (CheckUtils.checkChinese(goods.getProducer())) {
                return Result.error(500, "厂家不是中文/过长");
            } else if (goodsService.findByName(name) == null) {

                boolean result = goodsService.insert(goods);
                return result ? Result.ok() : Result.error(500, "添加失败");
            }
            return Result.error(400, "输入内容不能与已有内容重复");
        }
        return Result.error(400, "请选择商品类型");
    }

    /**
     * 更新编辑
     *
     * @param goods
     * @param session
     * @return 成功？失败？
     */

    @PostMapping("//goods/update")
    public Result<?> update(Goods goods, HttpSession session) {
        Integer goodsType = goods.getGoodsType();
        if (goodsType != null) {
            goods.setGoodsTypeUuid(goodsType);
            Object id = session.getAttribute("goodsUuid");
            goods.setUuid((Integer) id);
            String name = goods.getName();
            Double inPrice = goods.getInPrice();
            Double outPrice = goods.getOutPrice();
            String unit = goods.getUnit();
            if (name == null && inPrice == null && outPrice == null && unit == null ||
                    Objects.equals(name, str) || Objects.equals(unit, str)) {
                return Result.error(400, "请修输入全部参数");
            }
            //校验产地合法性
            else if (CheckUtils.checkChinese(goods.getOrigin())) {
                return Result.error(500, "产地不是中文/过长");
            }
            //校验厂家合法性
            else if (CheckUtils.checkChinese(goods.getProducer())) {
                return Result.error(500, "厂家不是中文/过长");
            }
            //校验名称合法性
            else if (CheckUtils.checkChinese(goods.getName())) {
                return Result.error(500, "名称不是中文/过长");
            } else if (goodsService.findByName(name) == null) {
                boolean result = goodsService.update(goods);
                return result ? Result.ok() : Result.error(500, "更新失败");
            }
            return Result.error(400, "输入内容不能与已有内容重复");
        }
        return Result.error(400, "请选择商品类型");
    }

    /**
     * 删除
     *
     * @param id
     * @return 成功？失败？
     */

    @DeleteMapping("/goods")
    public Result<?> delete(Integer id) {
        boolean delete = goodsService.delete(id);
        return delete ? Result.ok() : Result.error(500, "删除失败");
    }

}
